package com.springboot.test.common.utils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ThreadLocalRandom;

public class IdGenerator {
    public static String getNewId(String prefix) {
        StringBuilder sb = new StringBuilder();
        sb.append(prefix);
        sb.append(getCurrentTime());
//        sb.append("0000");
        sb.append(getUserIdRandomPart());
        return sb.toString();
    }

    private static String getUserIdRandomPart() {
        ThreadLocalRandom ra = ThreadLocalRandom.current();
        int randomNum = ra.nextInt(9999);
//        String result = "0000" + randomNum;
        String result = String.valueOf(randomNum);
//        result = result.substring(result.length() - 4);
        return result;
    }

    public static String getCurrentTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        return sdf.format(new Date());
    }
}
